package com.cardbaobao.cardbabyclient.utils;

import java.text.DecimalFormat;

/**
 * Created by Administrator on 2016/12/21.
 * 贷款计算帮助类
 */

public class LoansUtils {
    private static DecimalFormat df = new DecimalFormat("######0.00");

    /**
     * 获取每个月月供
     * @param loansAmount 贷款额度
     * @param managementCost 月管理费用
     * @param monthRate 月利率
     * @param loansMonth 贷款期数
     * @return
     */
    public static String getLoansMonth(double loansAmount,double managementCost,double monthRate,double loansMonth){
        double mLoansMonthCost = 0;
        mLoansMonthCost = getLoansMonthRate(loansAmount,loansMonth,monthRate);
        if(managementCost > 0 ){
            mLoansMonthCost = mLoansMonthCost + loansAmount * managementCost;
        }
        return df.format(mLoansMonthCost);
    }


    /**
     * 计算贷款外的金额
     * @param loansAmount
     * @param managementCost
     * @param monthRate
     * @param loansMonth
     * @param serviceCost
     * @return
     */
    public static String getLoansTotalCost(double loansAmount,double managementCost,double monthRate,double loansMonth ,double serviceCost){
        double mTotalCost = 0 ;
        double mLoansRate =  Double.parseDouble(df.format(((getLoansMonthRate(loansAmount,loansMonth,monthRate) * loansMonth) - loansAmount)));
        mTotalCost = mLoansRate + loansAmount * managementCost * loansMonth + loansAmount * serviceCost;
        return df.format(mTotalCost);
    }

    /**
     *月利率 月供
     * @param loansAmount 贷款额度
     * @param loansMonth 贷款月份
     * @param monthrate 月利率
     * @return
     */
    public static double getLoansMonthRate(double loansAmount,double loansMonth,double monthrate){
        if(monthrate == 0) return 0;
        double mLoansMonthRate = 0;
        mLoansMonthRate = loansAmount * monthrate * Math.pow(((double)1 + monthrate),loansMonth) / (Math.pow(((double)1 + monthrate),loansMonth) - (double)1);
        return mLoansMonthRate;
    }


    public static int getMsgCode(){
        return (int) ((Math.random()*9+1)*100000);
    }
}
